<?php
// Start session management and include necessary functions
session_start();
require_once('../dao/database.php');
require_once('../dao/user_db.php');
require_once '../dao/KilometerEntryDAO.php';
require_once ('../dao/user_db.php');

// Get the action to perform
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = 'show_result';
}

// Perform the specified action
switch($action) {
    case 'show_result':
        $totalSum = KilometerEntryDAO::getTotalSum();
        
        //list beginn
        $count = KilometerEntryDAO::countAllVotes();
        $gesamtPages = ceil(intval($count)/Constants::VAR_PAGE_SIZE);
        $newNextPage = 0;
        
        $from = 0;
        
        if (isset($_GET['page'])) {
            $nextPage = $_GET['page'];
            if (is_numeric($nextPage)){
                if (intval($nextPage) >= 0){ 
                        if ( intval($nextPage) < $gesamtPages ){
                            $from = $nextPage * Constants::VAR_PAGE_SIZE;
                            $newNextPage = $nextPage;
                        }
                }
            }
        }
        $entries = get_best_users($from,Constants::VAR_PAGE_SIZE);
        //list end
        
        include('resultView.php');
        break;
}
?>